//导入express
const express = require('express')
const {  exec } = require('../db/index')

//实例化router对象
const router = express.Router()
router.get('/', async (req, res) => {
  let sql
  // 解析请求参数
  sql = `SELECT COUNT(1) value,c.alias name FROM article a,categories c WHERE a.category_id=c.id GROUP BY a.category_id`
  const totalC = await exec(sql)
  // 根据分页公式, 计算offset偏移量
  // 操作数据库
  sql = `SELECT DATE_FORMAT(create_time, '%Y-%m-%d') create_time, COUNT(*) totalP FROM article GROUP BY DATE_FORMAT(create_time, '%Y-%m-%d') ORDER BY create_time DESC LIMIT 6 `
  
  const totalP = await exec(sql)

  res.send({
    code: 0,
    message: '获取文章成功',
    result: {
      totalC,
      totalP,
    }
  })
})
module.exports = router;